package com.gues.handler;

import com.gues.result.Result;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice
public class MyExceptionHandler {
    @ExceptionHandler(value = { AuthorizationException.class})
    public Result authorizationException(AuthorizationException e) {
        //获取错误中中括号的内容
        String message = e.getMessage();
        String msg=message.substring(message.indexOf("[")+1,message.indexOf("]"));
        //判断是角色错误还是权限错误

        Result result = new Result();
        result.setCode(400);

        if (message.contains("role")) {
            result.setMessage("对不起，您没有" + msg + "角色");
        } else if (message.contains("permission")) {
            result.setMessage("对不起，您没有" + msg + "权限");
        } else {
            result.setMessage("对不起，您的权限有误");
        }
        return result;
    }

    @ExceptionHandler(value = { UnknownAccountException.class})
    public Result unknownAccountException(UnknownAccountException e) {
        System.out.println("UnknownAccountException");
        return new Result(403, "账号或密码错误");
    }
}
